package com.passive.lock.service.user.imp;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.passive.lock.mapper.TUserMapper;
import com.passive.lock.mapper.TUserMapperCustom;
import com.passive.lock.model.po.TUser;
import com.passive.lock.model.po.TUserExample;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.log.SysLogService;
import com.passive.lock.service.user.UserService;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.MD5;
import com.passive.lock.utils.Result;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource(name = "TUserMapper")
	private TUserMapper userMapper;

	@Resource(name = "TUserMapperCustom")
	private TUserMapperCustom tUserMapperCustom;
	
	@Autowired
	private SysLogService sysLogService;
	
	 /**
     * value属性表示使用哪个缓存策略，缓存策略在ehcache.xml
    */
    public static final String DEMO_CACHE_NAME = "mytest";
    //这里的单引号不能少，否则会报错，被识别是一个对象;
    //public static final String CACHE_KEY = "'demoInfo'";
    @Cacheable(value=DEMO_CACHE_NAME,key="'aaaa'")
	@Override
	public Result userLogin(String token,TUserVo uservo, HttpServletRequest request, HttpServletResponse response) {
		 System.err.println("没有走缓存！!!!!!!!!!11");
		TUserExample example = new TUserExample();
		TUserExample.Criteria Criteria = example.createCriteria();
		Criteria.andAccountEqualTo(uservo.getAccount());
		List<TUser> list = userMapper.selectByExample(example);
		// 如果没有此用户
		if (null == list || list.size() == 0) {
			return Result.build(400, "用户名或密码错误");
		}
		TUser u = list.get(0);
		// 比对密码
		String pwd_db = u.getPassword();// md5密文
		String pwd_md5 = new MD5().getMD5ofStr(uservo.getPassword());
		if (!pwd_db.equalsIgnoreCase(pwd_md5)) {
			// 用户名或密码错误
			return Result.build(400, "用户名或密码错误");
		}
		u.setPassword(null);
		TUserVo tUserVo = new TUserVo();
		DozerMapperUtils.map(u, tUserVo);
		request.getSession().setAttribute("tUserVo", tUserVo);
		return Result.ok(u);
	}

	@Override
	public Result addUser(TUserVo uservo) {
		TUserExample example = new TUserExample();
		TUserExample.Criteria Criteria = example.createCriteria();
		Criteria.andAccountEqualTo(uservo.getAccount());
		List<TUser> list = userMapper.selectByExample(example);
		if (!CollectionUtils.isEmpty(list)) {
			return Result.build(400, "该账号已注册，请重新输入");
		}
		String pwd_md5 = new MD5().getMD5ofStr("123456");
		uservo.setPassword(pwd_md5);
		TUser user = new TUser();
		DozerMapperUtils.map(uservo, user);
		
		user.setCreatetime(new Date());
		userMapper.insert(user);
		return Result.ok();
	}

	@Override
	public List<TUserVo> userList(String groupid,TUserVo tUserVo, Integer offset, Integer limit) {
			List<TUserVo> list = tUserMapperCustom.userList(groupid,tUserVo, offset, limit);
			List<TUserVo> listvo = new ArrayList<>();
			for (TUserVo uservo : list) {
				uservo.setPassword(null);
				listvo.add(uservo);	
			}
			return listvo;
		
	}

	@Override
	public int userListCount(String groupid,TUserVo tUserVo) {
		try {
			int count = tUserMapperCustom.userListCount(groupid,tUserVo);
			return count;
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public Result updateUser(TUserVo uservo) {
		TUser user = userMapper.selectByPrimaryKey(uservo.getId());
		TUser tuser = new TUser();
		DozerMapperUtils.map(uservo, tuser);
		if(tuser.getPassword()==null||"".equals(tuser.getPassword())){
			tuser.setPassword(user.getPassword());
		}
	
		int count  = userMapper.updateByPrimaryKey(tuser);
		return Result.ok(count);
	}

	@Override
	public Result deleteUserList(List<Integer> userids) {
		int counts = 0;
		
		for (Integer id : userids) {
			userMapper.deleteByPrimaryKey(id);
			counts ++;
		}

		return Result.ok(counts);
	}

	@Override
	public Result deleteUser(TUserVo uservo,TUserVo user) {
		int count = userMapper.deleteByPrimaryKey(uservo.getId());
		sysLogService.addSysLog("2001", user.getId(), uservo.getId().toString(), user.getName(), "1", user.getGroupid(), count);
		return Result.ok(count);
	}

	@Override
	public Result queryUserList(String deptid) {
		TUserExample example = new TUserExample();
		TUserExample.Criteria Criteria = example.createCriteria();
		Criteria.andGroupidEqualTo(deptid);
		List<TUser> list = userMapper.selectByExample(example);
		List<TUserVo> listvo= this.pwdnull(list);
		return Result.ok(listvo);
	}
	
	/**
	 * 返回用户信息密码null
	 * 
	 */
	public List<TUserVo> pwdnull(List<TUser> list) {
		List<TUserVo> listvo = new ArrayList<TUserVo>();
		for (TUser tUser : list) {
			TUserVo userVo = new TUserVo();
			DozerMapperUtils.map(tUser, userVo);
			userVo.setPassword(null);
			listvo.add(userVo);
		}
		return listvo;
	}
	
	@Override
	public Result userListByGroupid(String groupid) {
		List<TUserVo> listvo = tUserMapperCustom.userListByGroupid(groupid);
		return Result.ok(listvo);
	}

	@Override
	public Result updatePassword(TUserVo uservo) {
		TUser user = userMapper.selectByPrimaryKey(uservo.getId());
		String pwd_md5 = new MD5().getMD5ofStr(uservo.getOldpassword());
		if(pwd_md5!=null&&pwd_md5.equals(user.getPassword())){
			String new_pwd_md5 = new MD5().getMD5ofStr(uservo.getPassword());
			user.setPassword(new_pwd_md5);
			userMapper.updateByPrimaryKey(user);
			return Result.ok();
		}
		return Result.build(400,"账号或老密码错误!");
	}

	@Override
	public Result checkPassword(TUserVo uservo) {
		TUser user = userMapper.selectByPrimaryKey(uservo.getId());
		String pwd_md5 = new MD5().getMD5ofStr(uservo.getOldpassword());
		if(pwd_md5!=null&&pwd_md5.equals(user.getPassword())){
			return Result.ok();
		}
		return Result.build(400,"账号或老密码错误!");
	}
	
	@Override
	public List<TUserVo> exportUser(String groupid, TUserVo tUserVo) {
		List<TUserVo> list = tUserMapperCustom.exportUserList(groupid,tUserVo);
		return list;
	}
}


